-- TD 4.7.3 Try.SNK DRP Connect Try.SRC DRP Test
baselib.report(0x00, "TD4.7.3 beginning.")

-- 初始化设置
busilib.setUsbCcs(CCS.Disabled)             -- 设置CVS为Disabled.SNK
busilib.setUsbRole(ROLE.SNK)
busilib.SetDAP(DAP.VBUS, 8100)              -- 设置VBUS为5V
busilib.SetOrientation(1)                   -- 设置USB的CC线,另一根则为Vconn线(这里设置CC1)
baselib.delay(10)

-- 1. CVS transitions to Unattached.SNK
busilib.setUsbCcs(CCS.Unattached)

-- 2. CVS waits 5ms
baselib.delay(5)

-- 3. CVS verifies:
-- 3.a. PUT presents Rp within tDRP – dcSRC.DRP x tDRP  [TD.4.7.3.V.1], 验证Rp在(tDRP - dcSRC_DRP*tDRP)时间内持续存在
baselib.delay(30)                     -- 等待tDRP - (tDRP * dcSRC_DRP)=100-70=30  
local VCC1 = busilib.GetADC(ADC.CC1, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mv")
if VCC1 > 200 then
    baselib.report(0x00, "TD4.7.3.V.1 pass.")
else
    baselib.report(0x00, "TD4.7.3.V.1 fail.")
end

-- 3.b PUT transitions from Rd to Rp within tDRPTransition[TD.4.7.3.V.2], 验证状态转换时间tDRPTransition ≤1ms


-- 4. CVS transitions to AttachWait.SNK
busilib.setUsbCcs(CCS.AttachWait)
baselib.delay(5)

-- 5. CVS verifies PUT keeps Rp attached since step 3 for tCCDebounce[TD.4.7.3.V.3], 验证Rp持续tCCDebounce时间
local monitor_rp_duration = busilib.newMonitor(TMD.CHECK_POINT, 0, {2}, {0}, {0})    -- 监控CC1的Rp状态
baselib.delay(200)     --tCCDebounce = 100-200ms
if busilib.getMonitorResult(monitor_rp_duration) == 1 then
    baselib.report(0x00, "TD.4.7.3.V.3 pass.")
else
    baselib.report(0x00, "TD.4.7.3.V.3 fail.")
end
busilib.deleteMonitor(monitor_rp_duration)

-- 6. CVS verifies PUT transitions to Try.SNK within tCCDebounce max + tDRPTransition since step 2: 
-- 6.a PUT presents Rd [TD.4.7.3.V.4], 通过检测PUT加载Rd来验证PUT转换到Try.SNK
baselib.delay(201)               --tCCDebounce max + tDRPTransition = 200 + 1 = 201
VCC1 = busilib.GetADC(ADC.CC1, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mv")
if VCC1 < 200 then
    baselib.report(0x00, "TD.4.7.3.V.4 pass.")
else
    baselib.report(0x00, "TD.4.7.3.V.4 fail.")
end

-- 7.  CVS transitions to Unattached.SRC after tPDDebounce, and on to AttachWait.SRC for tCCDebounce, 状态转换链
baselib.delay(20)   --tPDDebounce = 10-20ms
busilib.setUsbRole(ROLE.Source)
busilib.setUsbCcs(CCS.Unattached)
baselib.delay(200)  --tCCDebounce = 100-200ms
busilib.setUsbCcs(CCS.AttachWait)

-- 8. CVS transitions to Attached.SRC, CVS转换到Attached.SRC
busilib.setUsbCcs(CCS.Attached)
baselib.delay(5)

-- 9. CVS verifies that PUT transitions to Attached.SNK:  通过以下步骤来验证PUT转换到Attached.SNK
-- 9.a PUT sinks current according to CVS advertisement [TD.4.7.3.V.5].验证电流
if busilib.waitCondition4ad(ADC.VBUS_I, 20, 500, 5000, 1, 500) == LuaOk then
    baselib.report(0x00, "TD.4.7.3.V.5 fail. Overcurrent detected")
else
    baselib.report(0x00, "TD.4.7.3.V.5 pass.")
end

-- 9.b If PUT supports PD: i. PUT and CVS negotiate an explicit contract. [TD.4.7.3.V.6] PD协议协商（如果支持）
if USB_PD_Support then
    local PN_result = tcpclib.PresetPowerNegotiation(1, 500)
    if PN_result == LuaOk then
        baselib.report(0x00, "TD.4.7.3.V.6 pass.")
    else
        baselib.report(0x00, "TD.4.7.3.V.6 fail.")
    end
end

--9.c If VIF field PUT supports USB 3.1: i. PUT starts data communications on its SuperSpeed pairs. [TD.4.7.3.V.7] 

--9.d Else if PUT supports USB 2.0:i. PUT starts data communications on its D+/D- pair. [TD.4.7.3.V.8] 


-- 10. CVS transitions to Disabled, CVS转换到Disabled
busilib.setUsbCcs(CCS.Disabled)

-- 11. CVS verifies that PUT transitions to Unattached.SNK before tVBUSOFF expires: a. PUT data communication has ceased [TD.4.7.3.V.9], 验证PUT回到Unattached.SNK
baselib.delay(650)  -- tVBUSOFF


-- 清理资源
baselib.report(0x00, "TD4.7.3 completed.")
baselib.report(0x07, "\x04")